*Script to rewrite some e-variables in order to to compute marginal effects
*for the logit process

program transform, eclass
	version 11
	
	display "Note: cannot guarantee correctness if there is collinearity in the zinb model!"
	display "Warning: many options have been hard-coded; if you're running zinb with nondefault"
	display "options, you might want to check that those carry over."
	
	tempname a b w V ll constraints Cns oldcommandline clust touse grad gradient N N_clust rank k converged modelbased V_modelbased rules
	scalar `N'=e(N)
	scalar `k'=e(k)
	scalar `ll'=e(ll)
	scalar `converged'=e(converged)
	scalar `rank'=e(rank)
	scalar `N_clust'=e(N_clust)
	matrix `a'=(-1)*e(b)
	matrix `b'=`a'[1,"inflate:"]
	matrix `grad'=e(b)
	matrix `gradient'=`grad'[1,"inflate:"]
	matrix `w'=e(V)
	matrix `V'=`w'["inflate:","inflate:"]
	matrix `modelbased'=e(V_modelbased)
	matrix `V_modelbased'=`modelbased'["inflate:","inflate:"]
	*matrix `constraints'=e(Cns)
	*matrix `Cns'=`constraints'[2,"inflate:"]
	matrix `rules'=(0,0,0,0)
	local `clust'="`e(clustvar)'"
	local `oldcommandline'=subinstr("`e(cmdline)'","zinb","logit",.)
	gen `touse'=e(sample)
	
	ereturn post `b' `V', esample(`touse') /*deletes all existing e results*/
	*makecns `Cns', display
	
	ereturn matrix gradient `gradient'
	ereturn matrix V_modelbased `V_modelbased'
	ereturn matrix rules `rules'
	ereturn scalar N=`N'
	ereturn scalar k=`k'
	ereturn scalar converged=`converged'
	ereturn scalar rank=`rank'
	ereturn scalar N_clust=`N_clust'
	ereturn scalar ll=`ll'
		
	ereturn local cmdline "``oldcommandline''"
	ereturn local cmd "logit"
	ereturn local predict "logit_p"
	ereturn local estat_cmd "logit_estat"
	ereturn local marginsnotok "stdp DBeta DEviance DX2 DDeviance Hat Number Residuals RStandard SCore"
	ereturn local title "Logistic regression"
	ereturn local user "mopt__logit_e2()"
	ereturn local chi2type "Wald"
	ereturn local opt "moptimize"
	ereturn local vcetype "Robust"
	ereturn local clustvar "``clust''"
	ereturn local vce "cluster"
	ereturn local title "Logistic regression"
	ereturn local crittype "log pseudolikelihood"
	ereturn local ml_method "e2"
	ereturn local singularHmethod "m-marquardt"
	ereturn local technique "nr"
	ereturn local which "max"
	ereturn local properties "b V"
	ereturn local depvar "inflate"

	display "-----------------------------------------------------------------------------------------"
	display "Transform success: It now looks as if a Logit model (cmd logit) has been estimated with the"
	display "parameters of the inflate process of the zero inflated model."
	display "Note that the point estimates have been inverted, so that a positive sign represents a positive effect."
	
end


















